<?php
	class Modules_Projects_View_ListProjects
	{
		private $JS;
		var $selection=0;
		public function __construct()
		{
			
		}
		public static function MakeAction($id)
		{
			$str="";
			$SetDefault='GetData({"module":"Projects.ChangeDefault","project":"'.$id.'"}, f=function(response)
			{
				var result=JSON.parse(response);
				if(result["result"]==1)
					location="index.php"
				else
					alert("No permissions");
			});';
			$str.="<span class=\"buttonDefault\" onclick='".$SetDefault."'></span>";
			
			$SetDefault='GetData({"module":"Projects.RemoveProject","project":"'.$id.'"}, f=function(response)
			{
				var result=JSON.parse(response);
				if(result["result"]==1)
					location="index.php"
				else
					alert("No permissions");
			});';
			$str.="<span class=\"buttonRemove\" onclick='".$SetDefault."'></span>";
			return $str;
		}
		public function draw()
		{
			$str=file_get_contents(dirname(__FILE__)."/ListProjects.html");
			
			$fields=array("Project","Client","Creator","Next Milestone","Actions");
			$header=new Modules_Common_Design_RowsHeader($fields);
			$table= new Modules_Common_Design_Table($header);
			$workspaces=Modules_Common_DB_WorkspaceHasUsers::ByUser($_SESSION['id']);
			foreach($workspaces as $ws)
			{
				$result=Modules_Common_DB_Projects::SearchByWorkspace($ws->workspace_id);
				foreach($result as $value)
				{
					$rows=new Modules_Common_Design_Rows($header);
					$row=array(
					"Project"=>$value->name,
					"Client"=>"cliente",
					"Creator"=>$value->creator,
					"Next Milestone"=>$value->getNext,
					"Actions"=>self::MakeAction($value->id)
					);
					$rows->setRow($row);
					$table->AddRows($rows);
				}
				$workspace=new Modules_Common_DB_Workspace($ws->workspace_id);
				$resulta.="<h2>".$workspace->name."<span class=\"buttonRemove\" onclick='".$ws->workspace_id."'></span></h2>".$table->draw()."";
			}
			
			$str=str_replace("<$=DATA=$>",$resulta,$str);
			
		
		
			echo $str;
			
		}
		public function LateralMenu()
		{
			$lateral=new Modules_Projects_View_Menu($this->selection);	
			return $lateral;	
		}
		public function getJS()
		{
			return $this->JS;
		}
	}
	?>